package com.zh.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.manage.dao.AreaMapper;
import com.zh.manage.entity.AreaEntity;
import com.zh.manage.service.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 地区管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/8/16
 * @Version: 1.0.1
 */
@Api(value = "地区管理控制器", produces = "application/json", tags = {"地区管理控制器"})
@RestController
@RequestMapping("/area")
public class AreaController {

    /**
     * @Description: 简易crud时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/17
     */
    @Autowired
    private AreaMapper areaMapper;

    /**
     * @Description: 有业务逻辑需要处理时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/17
     */
    @Autowired
    private AreaService areaService;

    /**
     * @Description: redis实例
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/17
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Description: mq实例
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/8/17
     */
    @Autowired
    private AmqpTemplate amqpTemplate;


    static JSONResult<Object> getObjectJSONResult(boolean b, @ApiParam(value = "地区实体类", name = "areaEntity") AreaEntity areaEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (b) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "新增一条记录", tags = {"putArea"}, notes = "新增一条记录")
    @PutMapping("/putArea")
    public JSONResult<Object> putArea(@ApiParam(value = "地区实体类", name = "areaEntity") AreaEntity areaEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (areaEntity.insert()) {
                // 返回新增主键
//                System.out.println(areaEntity.getId());
                // 添加时往队列添加一条条数据
                amqpTemplate.convertAndSend("areaRabbit", "新增一个地区");
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"updateAreaById"}, notes = "通过id修改信息")
    @PutMapping("/updateAreaById")
    public JSONResult<Object> updateAreaById(@ApiParam(value = "地区实体类", name = "areaEntity") AreaEntity areaEntity) {
        return getObjectJSONResult(areaEntity.updateById(), areaEntity);
    }

    @ApiOperation(value = "根据主键删除记录", tags = {"deleteAreaById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteAreaById")
    public JSONResult<Object> deleteAreaById(@ApiParam(value = "地区实体类", name = "areaEntity") AreaEntity areaEntity) {
        return getObjectJSONResult(areaEntity.deleteById(), areaEntity);
    }


    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteAreaBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteAreaBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteAreaBatchByIds(@ApiParam(value = "主键集合", name = "ids") @RequestBody List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.areaMapper.deleteBatchIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据id查询详细信息", tags = {"getAreaById"}, notes = "根据id查询详细信息")
    @GetMapping("/getAreaById")
    public JSONResult<AreaEntity> getAreaById(@ApiParam(value = "地区实体类", name = "areaEntity") AreaEntity areaEntity) {
        JSONResult<AreaEntity> jsonResult = new JSONResult<>();
        try {
            AreaEntity result = areaEntity.selectById(areaEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesAreas"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesAreas")
    public JSONResult<List<AreaEntity>> getPagesAreas(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "显示数量页数", name = "pageSize")Integer pageSize,
            @ApiParam(value = "地区实体类", name = "areaEntity") AreaEntity areaEntity) {
        JSONResult<List<AreaEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<AreaEntity> page = new Page<>(pageNo, pageSize);
            QueryWrapper<AreaEntity> wrapper = new QueryWrapper<>();
            wrapper.setEntity(areaEntity);
            IPage<AreaEntity> result = this.areaMapper.selectPage(page, wrapper);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    /*@GetMapping("/getAll")
    public JSONResult<Object> selectAll() {
        System.out.println(1);
        return new JSONResult<Object>(this.areaService.getById(37), HttpStatus.OK.getReasonPhrase(), true);
    }*/
}
